/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package View;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.table.DefaultTableModel;
import model.DAO.CallsDAO;
import model.DAO.CallsRoleDAO;
import model.DAO.CustomerDAO;
import model.DAO.RoomDAO;
import model.entities.Calls;
import model.entities.CallsRole;
import model.entities.Customer;
import model.entities.Room;

/**
 *
 * @author Hieu.Nguyen
 */
public class FrmCallsManagement extends javax.swing.JPanel {

    /**
     * Creates new form QuanLyCuocGoi
     */
    public FrmCallsManagement() {
        initComponents();
        loadTableDichVu(refreshCallsList());
        loadCBORoleCG();
        loadCBOCustomerName();
        loadCBORoomNumber();
    }
    public List<Calls> refreshCallsList() {
        
       List<Calls> callsList;
       callsList = model.DAO.CallsDAO.getInstance().getCalls(true);

        return callsList;
    }
        public void loadCBORoleCG() {
            List<CallsRole> listRLG = CallsRoleDAO.getInstance().getCallsRole(true);
            for (CallsRole ro : listRLG) {
            cboCallsRole.addItem(ro.getCallsRoleID()+ "-" + ro.getCallsRoleName());
        }
    }
    
    public void loadCBOCustomerName() {
            List<Customer> listCustomer = CustomerDAO.getInstance().getCustomer(true);
            for (Customer ro : listCustomer) {
            cboCustomer.addItem(ro.getCustomerID()+ "-" + ro.getFirstName()+" "+ro.getMiddleName()+" "+ro.getLastName());
        }
    }
    
    public void loadCBORoomNumber() {
            List<Room> listRoom = RoomDAO.getInstance().getRoom(true);
            for (Room ro : listRoom) {
            cboRoomNumber.addItem(ro.getRoomID() +"-"+ ro.getRoomNumber());
        }
    }

    public void binding() throws ParseException{
    
      int rowIndex = tblCallMgmt.getSelectedRow();

            String callsID = (String) tblCallMgmt.getValueAt(rowIndex, 1);
            txtCallsID.setText(callsID);
            
            int roomID = CallsDAO.getInstance().getCallsByID(Integer.parseInt(callsID)).getRoom().getRoomID();
            String RoomType = CallsDAO.getInstance().getCallsByID(Integer.parseInt(callsID)).getRoom().getRoomNumber();
            cboRoomNumber.setSelectedItem(roomID + "-" + RoomType);
            
            int callR = CallsDAO.getInstance().getCallsByID(Integer.parseInt(callsID)).getCalsRole().getCallsRoleID();
            String callN = CallsDAO.getInstance().getCallsByID(Integer.parseInt(callsID)).getCalsRole().getCallsRoleName();
            cboRoomNumber.setSelectedItem(callR + "-" + callN);
            
            int cusID = CallsDAO.getInstance().getCallsByID(Integer.parseInt(callsID)).getCustomer().getCustomerID();
            String cusFName = CallsDAO.getInstance().getCallsByID(Integer.parseInt(callsID)).getCustomer().getFirstName();
            String cusMName = CallsDAO.getInstance().getCallsByID(Integer.parseInt(callsID)).getCustomer().getMiddleName();
            String cusLName = CallsDAO.getInstance().getCallsByID(Integer.parseInt(callsID)).getCustomer().getLastName();
            cboCustomer.setSelectedItem(cusID+ "-" + cusFName+" "+cusMName+" "+cusLName);  
            
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            if (tblCallMgmt.getValueAt(rowIndex, 5) != null) {
                dtCallDateStart.setDate(df.parse((String) tblCallMgmt.getValueAt(rowIndex, 5)));
            }
            
            SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
            if (tblCallMgmt.getValueAt(rowIndex, 7) != null) {
                dtCallsDateEnd.setDate(df.parse((String) tblCallMgmt.getValueAt(rowIndex, 7)));
            }
            //String timeStart =(String) spnTimeStart.getV
   
}
    public void loadTableDichVu(List<Calls> list) {
        DefaultTableModel dtm = (DefaultTableModel) tblCallMgmt.getModel();
        String[] colNames = {"CallsID", "RoomNumber","CallsRoleName", "Customer","CallsDateStart","TimeStart","CallsDateEnd","TimeEnd","CallsContent"};
        dtm.setColumnIdentifiers(colNames);
        dtm.setNumRows(0);

        for (Calls stud : list) {
            String[] cells = new String[9];
            cells[0] = String.valueOf(stud.getCallsID());
            cells[1] = String.valueOf(stud.getRoom().getRoomID()+"-"+stud.getRoom().getRoomNumber());
            cells[2] = String.valueOf(stud.getCalsRole().getCallsRoleID()+"-"+stud.getCalsRole().getCallsRoleName());
            cells[3] = String.valueOf(stud.getCustomer().getCustomerID()+"-"+stud.getCustomer().getLastName()+"-"+stud.getCustomer().getMiddleName()+"-"+stud.getCustomer().getLastName());
            cells[4] = String.valueOf(stud.getCallsDateStart());
            cells[5] = String.valueOf(stud.getTimeStart());
            cells[6] = String.valueOf(stud.getCallsDateEnd());
            cells[7] = String.valueOf(stud.getTimeEnd());
            cells[8] = String.valueOf(stud.getCallsContent());
            
            dtm.addRow(cells);
        }
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblCallMgmt = new javax.swing.JTable();
        btnAdd = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        lblTimeStart = new javax.swing.JLabel();
        lblCallsDateEnd = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtCallsID = new javax.swing.JTextField();
        cboRoomNumber = new javax.swing.JComboBox();
        cboCallsRole = new javax.swing.JComboBox();
        cboCustomer = new javax.swing.JComboBox();
        dtCallsDateEnd = new com.toedter.calendar.JDateChooser();
        dtCallDateStart = new com.toedter.calendar.JDateChooser();
        java.util.Date date1 = new java.util.Date();
        SpinnerDateModel sm2 =
        new SpinnerDateModel(date1, null, null, Calendar.HOUR_OF_DAY);
        spnTimeStart = new javax.swing.JSpinner(sm2);
        java.util.Date date2 = new java.util.Date();
        SpinnerDateModel sm1 =
        new SpinnerDateModel(date2, null, null, Calendar.HOUR_OF_DAY);
        sprTimeEnd = new javax.swing.JSpinner(sm1);
        jScrollPane2 = new javax.swing.JScrollPane();
        txtCallsContent = new javax.swing.JTextArea();
        btnSave = new javax.swing.JButton();
        btnCanel = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        jLabel1.setText("Calls Management");

        tblCallMgmt.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "CallsID", "RoomNumber", "CallsRole", "Customer", "CallsDateStart", "TimeStart", "CallsDateEnd", "TimeEnd", "CallsContent"
            }
        ));
        tblCallMgmt.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                tblCallMgmtAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        jScrollPane1.setViewportView(tblCallMgmt);

        btnAdd.setText("ADD");
        btnAdd.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnAddMouseClicked(evt);
            }
        });
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnDelete.setText("DELETE");

        btnEdit.setText("EDIT");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        jLabel2.setText("CallsID           :");

        jLabel3.setText("RoomNumber :");

        jLabel4.setText("CallsRole        :");

        jLabel5.setText("Customer       :");

        jLabel6.setText("CallsDateStart :");

        lblTimeStart.setText("TimeStart        :");

        lblCallsDateEnd.setText("CallsDateEnd   :");

        jLabel9.setText("TimeEnd           :");

        jLabel10.setText("CallsContent :");

        JSpinner.DateEditor de1 = new JSpinner.DateEditor(spnTimeStart, "HH:MM:ss");
        spnTimeStart.setEditor(de1);

        JSpinner.DateEditor de = new JSpinner.DateEditor(sprTimeEnd, "HH:MM:ss");
        sprTimeEnd.setEditor(de);

        txtCallsContent.setColumns(20);
        txtCallsContent.setRows(5);
        jScrollPane2.setViewportView(txtCallsContent);

        btnSave.setText("Save");

        btnCanel.setText("Canel");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(65, 65, 65)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel3)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(cboRoomNumber, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel4)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(cboCallsRole, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel5)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(cboCustomer, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(txtCallsID, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGap(72, 72, 72)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lblTimeStart, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lblCallsDateEnd, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGap(35, 35, 35)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(dtCallDateStart, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(sprTimeEnd, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(spnTimeStart, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(dtCallsDateEnd, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)))
                            .addGap(41, 41, 41)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel10)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(375, 375, 375)
                            .addComponent(jLabel1)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(31, 31, 31)
                        .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCanel, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(251, 251, 251)))
                .addContainerGap(39, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(17, 17, 17)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(38, 38, 38)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel6)
                                    .addComponent(txtCallsID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(dtCallDateStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(lblTimeStart)
                            .addComponent(cboRoomNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(spnTimeStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel4)
                                .addComponent(lblCallsDateEnd)
                                .addComponent(cboCallsRole, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(dtCallsDateEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jLabel9)
                            .addComponent(cboCustomer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(sprTimeEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(308, 308, 308)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 66, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCanel, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
//        ThemQLCuocGoi objTQLCG = new ThemQLCuocGoi();
//        objTQLCG.setVisible(true);
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
//        ChinhSuaQLCuocGoi objCSQLCG = new ChinhSuaQLCuocGoi();
//        objCSQLCG.setVisible(true);
    }//GEN-LAST:event_btnEditActionPerformed

    private void btnAddMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnAddMouseClicked
        // TODO add your handling code here:
//        resetComponent();
//        setEnableComponent(true);
    }//GEN-LAST:event_btnAddMouseClicked

    private void tblCallMgmtAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_tblCallMgmtAncestorAdded
        // TODO add your handling code here:

    }//GEN-LAST:event_tblCallMgmtAncestorAdded


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCanel;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cboCallsRole;
    private javax.swing.JComboBox cboCustomer;
    private javax.swing.JComboBox cboRoomNumber;
    private com.toedter.calendar.JDateChooser dtCallDateStart;
    private com.toedter.calendar.JDateChooser dtCallsDateEnd;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblCallsDateEnd;
    private javax.swing.JLabel lblTimeStart;
    private javax.swing.JSpinner spnTimeStart;
    private javax.swing.JSpinner sprTimeEnd;
    private javax.swing.JTable tblCallMgmt;
    private javax.swing.JTextArea txtCallsContent;
    private javax.swing.JTextField txtCallsID;
    // End of variables declaration//GEN-END:variables
}
